package com.codinghero.zqlb.weibo.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import weibo4j.Oauth;
import weibo4j.http.AccessToken;
import weibo4j.model.WeiboException;

import com.codinghero.core.bo.NameValBo;

public class LoginWeiboServlet extends HttpServlet {

	protected static Logger log = Logger.getLogger(LoginWeiboServlet.class);
	
	private static final long serialVersionUID = 1L;

	private NameValBo nameValBo = new NameValBo();
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
		Oauth oauth = new Oauth();
		try {
			AccessToken accessToken = oauth.getAccessTokenByCode(req.getParameter("code"));
			log.info("accessToken=" + accessToken.getAccessToken());
			nameValBo.saveNameVal("ACCESS_TOKEN", accessToken.getAccessToken());
		} catch (WeiboException e) {
			e.printStackTrace();
			resp.setCharacterEncoding("UTF-8");
			resp.getWriter().println("新浪微博登陆暂时不可用，请刷新页面");
		}
	}
}
